//
//  DHSelectTableViewCell.m
//  DHKuaiYi
//
//  Created by  chenhanmao on 2016/11/13.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHSelectTableViewCell.h"
#import <Masonry.h>

@interface DHSelectTableViewCell ()

/**  isBtn  */
@property(nonatomic,weak) UIButton *isButton;

/**  noBtn  */
@property(nonatomic,weak) UIButton *noButton;

/**  label  */
@property(nonatomic,weak) UILabel *label;

@end

@implementation DHSelectTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Setter 模型驱动
- (void)setLabelText:(NSString *)labelText {
    
    self.label.text = labelText;
    
}

- (void)setIsText:(NSString *)isText {
    
    [self.isButton setTitle:isText forState:UIControlStateNormal];
}

-(void)setNoText:(NSString *)noText {
    
    [self.noButton setTitle:noText forState:UIControlStateNormal];
}

#pragma mark - 界面布局
- (void)setupUI {
    
    UILabel *label = [[UILabel alloc] init];
    self.label = label;
    [self.contentView addSubview:label];
    label.font = [UIFont systemFontOfSize:14];
    label.text = @"是否确诊";
    label.textColor = [UIColor XHH_colorWithHex:0xbfbfbf];
    
    UIButton *isBtn = [self buttonWithTitle:@"已确诊"];
    self.isButton = isBtn;
    [self.contentView addSubview:isBtn];
    [isBtn addTarget:self action:@selector(selectedYES) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *noBtn = [self buttonWithTitle:@"症状疑似"];
    self.noButton = noBtn;
    [self.contentView addSubview:noBtn];
    [noBtn addTarget:self action:@selector(selectedNO) forControlEvents:UIControlEventTouchUpInside];
    
    /*------------------自动布局------------------*/
    CGFloat marginLeft = 10;
    CGFloat marginRight = 10;
    CGFloat buttonWidth = 90;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(marginLeft);
        make.centerY.equalTo(self.contentView);
    }];
    
    [noBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.contentView).offset(-marginRight);
        make.top.equalTo(self.contentView).offset(marginRight*0.5);
        make.bottom.equalTo(self.contentView).offset(-marginRight*0.5);
        make.width.mas_equalTo(buttonWidth);
    }];
    
    [isBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(noBtn.mas_left).offset(-marginLeft);
        make.top.equalTo(self.contentView).offset(marginRight*0.5);
        make.bottom.equalTo(self.contentView).offset(-marginRight*0.5);
        make.width.mas_equalTo(buttonWidth);
    }];
    
}

#pragma mark - 响应事件
//确认事件
- (void)selectedYES {
    
    self.isButton.selected = YES;
    self.noButton.selected = NO;
    
    if (self.selectedBlock) {
        
        self.selectedBlock(YES);
    }
    
}

//否认事件
- (void)selectedNO {
    
    self.isButton.selected = NO;
    self.noButton.selected = YES;
    
    if (self.selectedBlock) {
        
        self.selectedBlock(NO);
    }
}

#pragma mark - 创建可以点击的Button方法
- (UIButton *)buttonWithTitle:(NSString *)title {
    
    UIButton *btn = [self buttonWithTitle:title norImageName:@"select_default" selectImageName:@"select_click"];
    return btn;
}

- (UIButton *)buttonWithTitle:(NSString *)title norImageName:(NSString *)norImageName selectImageName:(NSString *)selectImageName {
    UIButton *btn = [[UIButton alloc] init];
    
    [btn setImage:[UIImage imageNamed:norImageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //图片调整
    [btn setImageEdgeInsets:UIEdgeInsetsMake(5, -3, 5, -3)];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    return btn;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
